#pragma once

#include <iostream>
#include <functional>
#include <string>
#include <cassert>
#include "Log.hpp"
#include "Error.hpp"

// class EpollServer;

class Connection
{
private:
    using call_back_t = std::function<void(Connection *)>;
    const static int defaultFd = -1;
    const static int defaultPort = 8888;

public:
    Connection(int fd = defaultFd) : _fd(fd)
    {
    }

    Connection(int fd, uint32_t events,
               const std::string &clientIp, const uint16_t &clientPort)
        : _fd(fd), _events(events), _clientIp(clientIp), _clientPort(clientPort),
          _readCallBack(nullptr), _writeCallBack(nullptr), _exceptionCallBack(nullptr)
    {
    }

    ~Connection() {}

    void registerCallBack(const call_back_t &readCallBack,
                          const call_back_t &writeCallBack,
                          const call_back_t &exceptionCallBack)
    {
        _readCallBack = readCallBack;
        _writeCallBack = writeCallBack;
        _exceptionCallBack = exceptionCallBack;
    }

public:
    // IO 信息
    int _fd;
    std::string _inBuffer; 
    std::string _outBuffer;

    // IO 方法 -- 处理读、写和异常事件
    call_back_t _readCallBack;
    call_back_t _writeCallBack;
    call_back_t _exceptionCallBack;

    // 客户相关信息 -- for Debug
    std::string _clientIp;
    uint16_t _clientPort;

    // 管理需要关心的事件
    uint32_t _events;
};